<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="预约单号" prop="reservationNo">
        <el-input
          v-model="queryParams.reservationNo"
          placeholder="请输入预约单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约状态" prop="states">
        <el-select v-model="queryParams.states" placeholder="请选择预约状态" clearable>
          <el-option
            v-for="dict in dict.type.repair_reservation_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item label="经销商id" prop="dealerId">-->
<!--        <el-input-->
<!--          v-model="queryParams.dealerId"-->
<!--          placeholder="请输入经销商id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="经销商名" prop="dealerName">
        <el-input
          v-model="queryParams.dealerName"
          placeholder="请输入经销商名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="车牌号" prop="license">
        <el-input
          v-model="queryParams.license"
          placeholder="请输入车牌号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="服务项目" prop="serviceType">
        <el-select v-model="queryParams.serviceType" placeholder="请选择服务项目" clearable>
          <el-option
            v-for="dict in dict.type.repair_service_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="姓名" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="预约时间" prop="reservationDate">
        <el-date-picker clearable
          v-model="queryParams.reservationDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择预约时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="vin" prop="vin">
        <el-input
          v-model="queryParams.vin"
          placeholder="请输入vin"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="二手车" prop="isUsedCar">
        <el-select v-model="queryParams.isUsedCar" placeholder="请选择是否二手车" clearable>
          <el-option
            v-for="dict in dict.type.no_and_yes"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核人名" prop="auditorName">
        <el-input
          v-model="queryParams.auditorName"
          placeholder="请输入审核人名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核时间" prop="auditorTime">
        <el-date-picker clearable
          v-model="queryParams.auditorTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择审核时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['aftersale:repairReservation:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['aftersale:repairReservation:edit']"-->
<!--        >审核</el-button>-->
<!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['aftersale:repairReservation:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['aftersale:repairReservation:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repairReservationList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="预约单号" align="center" prop="reservationNo" />
      <el-table-column label="预约状态" align="center" prop="states">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.repair_reservation_status" :value="scope.row.states"/>
        </template>
      </el-table-column>
      <el-table-column label="经销商id" align="center" prop="dealerId" />
      <el-table-column label="经销商名称" align="center" prop="dealerName" />
      <el-table-column label="车牌号" align="center" prop="license" />
      <el-table-column label="服务项目" align="center" prop="serviceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.repair_service_type" :value="scope.row.serviceType"/>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="username" />
      <el-table-column label="电话" align="center" prop="phone" />
      <el-table-column label="预约时间" align="center" prop="reservationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.reservationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="vin" align="center" prop="vin" />
      <el-table-column label="是否二手车" align="center" prop="isUsedCar">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.no_and_yes" :value="scope.row.isUsedCar"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="审核人名称" align="center" prop="auditorName" />
      <el-table-column label="审核时间" align="center" prop="auditorTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditorTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['aftersale:repairReservation:edit']"
            v-if="scope.row.states == 1"
          >客户到店</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['aftersale:repairReservation:remove']"-->
<!--          >删除</el-button>-->
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改维修预约对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="预约单号" prop="reservationNo">
          <el-input v-model="form.reservationNo" placeholder="请输入预约单号(若不填写，后台自动生成)" :disabled="title!='添加维修预约'"/>
        </el-form-item>
        <el-form-item label="预约状态" prop="states">
          <el-select v-model="form.states" placeholder="请选择预约状态" >
            <el-option
              v-for="dict in dict.type.repair_reservation_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工单id" prop="roId">
          <el-input v-model="form.roId" placeholder="请输入维修工单id" :disabled="true"/>
        </el-form-item>
        <el-form-item label="车牌号" prop="license">
          <el-input v-model="form.license" placeholder="请输入车牌号" :disabled="title!='添加维修预约'"/>
        </el-form-item>
        <el-form-item label="服务项目" prop="serviceType">
          <el-select v-model="form.serviceType" placeholder="请选择服务项目" :disabled="title!='添加维修预约'">
            <el-option
              v-for="dict in dict.type.repair_service_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="姓名" prop="username">
          <el-input v-model="form.username" placeholder="请输入姓名" :disabled="title!='添加维修预约'"/>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话" :disabled="title!='添加维修预约'"/>
        </el-form-item>
        <el-form-item label="预约时间" prop="reservationDate">
          <el-date-picker clearable
            v-model="form.reservationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择预约时间"
            :disabled="title!='添加维修预约'"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="vin" prop="vin">
          <el-input v-model="form.vin" placeholder="请输入vin" :disabled="title!='添加维修预约'"/>
        </el-form-item>
        <el-form-item label="二手车" prop="isUsedCar">
          <el-select v-model="form.isUsedCar" placeholder="请选择是否二手车" :disabled="title!='添加维修预约'">
            <el-option
              v-for="dict in dict.type.no_and_yes"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
<!--        <el-form-item label="审核人名称" prop="auditorName">-->
<!--          <el-input v-model="form.auditorName" placeholder="请输入审核人名称" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="审核时间" prop="auditorTime">-->
<!--          <el-date-picker clearable-->
<!--            v-model="form.auditorTime"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="请选择审核时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRepairReservation, getRepairReservation, delRepairReservation, addRepairReservation, updateRepairReservation } from "@/api/aftersale/repairReservation";

export default {
  name: "RepairReservation",
  dicts: ['repair_reservation_status', 'no_and_yes', 'repair_service_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 维修预约表格数据
      repairReservationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        reservationNo: null,
        states: null,
        dealerId: null,
        dealerName: null,
        license: null,
        serviceType: null,
        username: null,
        phone: null,
        reservationDate: null,
        vin: null,
        isUsedCar: null,
        auditorName: null,
        auditorTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // reservationNo: [
        //   { required: true, message: "预约单号不能为空", trigger: "blur" }
        // ],
        states: [
          { required: true, message: "预约状态不能为空", trigger: "change" }
        ],
        license: [
          { required: true, message: "车牌号不能为空", trigger: "blur" }
        ],
        serviceType: [
          { required: true, message: "服务项目不能为空", trigger: "change" }
        ],
        username: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "电话不能为空", trigger: "blur" }
        ],
        reservationDate: [
          { required: true, message: "预约时间不能为空", trigger: "blur" }
        ],
        isUsedCar: [
          { required: true, message: "是否二手车不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询维修预约列表 */
    getList() {
      this.loading = true;
      listRepairReservation(this.queryParams).then(response => {
        this.repairReservationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        reservationNo: null,
        states: null,
        roId: null,
        dealerId: null,
        dealerName: null,
        license: null,
        serviceType: null,
        username: null,
        phone: null,
        reservationDate: null,
        vin: null,
        isUsedCar: null,
        remark: null,
        auditorId: null,
        auditorName: null,
        auditorTime: null,
        createTime: null,
        updateTime: null,
        createBy: null,
        updateBy: null,
        createUserName: null,
        updateUserName: null,
        version: null,
        deleteFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加维修预约";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRepairReservation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改维修预约";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRepairReservation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRepairReservation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除维修预约编号为"' + ids + '"的数据项？').then(function() {
        return delRepairReservation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('aftersale/repairReservation/export', {
        ...this.queryParams
      }, `repairReservation_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
